/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary_package;

import Entitys.ProductoGeneral;
import Helpers.*;
import control_package.AutoCompleteControl;
import control_package.BussinesControl;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.TableColumn;
import javax.swing.text.BadLocationException;

/**
 *
 * @author Administrador
 */
public class SellGui extends javax.swing.JPanel {
   
//     private ArrayList<Object> b=new ArrayList<Object>(); para ver
     private VentaTableModel tableModel=new VentaTableModel();
     private AutoCompleteControl aCControl=new AutoCompleteControl();
     private BussinesControl buscon=new BussinesControl();
     private JList listProducts=new JList();
      
     
     private String resultado;
    /**
     * Creates new form SellGui
     */
    public SellGui() {
       
//        b.add(b); para ver despues
        initComponents();
        this.TableProducts.setModel(tableModel);
        fillButtons();
        Integrity();
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        TotalTextField = new javax.swing.JFormattedTextField();
        AddButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        QuantyFormattedTextField3 = new javax.swing.JFormattedTextField();
        QueryButton = new javax.swing.JButton();
        SubTotalTextField = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TableProducts = new javax.swing.JTable();
        IVATextField = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        NameTextField1 = new javax.swing.JFormattedTextField();
        IdFormattedTextField = new javax.swing.JFormattedTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        MakeSellButton = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "VENTA", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Verdana", 0, 18))); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(1240, 640));

        TotalTextField.setBackground(new java.awt.Color(0, 0, 0));
        TotalTextField.setEditable(false);
        TotalTextField.setForeground(new java.awt.Color(51, 255, 51));
        TotalTextField.setText("0");
        TotalTextField.setEnabled(false);
        TotalTextField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        TotalTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TotalTextFieldActionPerformed(evt);
            }
        });

        AddButton.setBackground(java.awt.Color.white);
        AddButton.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        AddButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/downarrow.png"))); // NOI18N
        AddButton.setText("Ingresar");
        AddButton.setPreferredSize(new java.awt.Dimension(183, 70));
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel4.setText("Subtotal:");

        jLabel3.setBackground(java.awt.Color.white);
        jLabel3.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel3.setText("cant:");

        QuantyFormattedTextField3.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N

        QueryButton.setBackground(java.awt.Color.white);
        QueryButton.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        QueryButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/xeyes.png"))); // NOI18N
        QueryButton.setText("Consultar");
        QueryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                QueryButtonActionPerformed(evt);
            }
        });

        SubTotalTextField.setBackground(new java.awt.Color(0, 0, 0));
        SubTotalTextField.setEditable(false);
        SubTotalTextField.setForeground(new java.awt.Color(51, 255, 51));
        SubTotalTextField.setText("0");
        SubTotalTextField.setEnabled(false);
        SubTotalTextField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N

        jLabel1.setBackground(java.awt.Color.white);
        jLabel1.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel1.setText("Nombre producto:");

        TableProducts.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        TableProducts.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(TableProducts);

        IVATextField.setBackground(new java.awt.Color(0, 0, 0));
        IVATextField.setEditable(false);
        IVATextField.setForeground(new java.awt.Color(51, 255, 51));
        IVATextField.setText("0");
        IVATextField.setEnabled(false);
        IVATextField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        IVATextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IVATextFieldActionPerformed(evt);
            }
        });

        jLabel2.setBackground(java.awt.Color.white);
        jLabel2.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel2.setText("Id:");

        NameTextField1.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        NameTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                NameTextField1FocusLost(evt);
            }
        });
        NameTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                NameTextField1KeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                NameTextField1KeyReleased(evt);
            }
        });

        IdFormattedTextField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        IdFormattedTextField.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                IdFormattedTextFieldMouseMoved(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel5.setText("IVA:");

        jLabel6.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel6.setText("Total:");

        MakeSellButton.setBackground(java.awt.Color.white);
        MakeSellButton.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        MakeSellButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/Copia de icon_check.png"))); // NOI18N
        MakeSellButton.setText("VENTA");
        MakeSellButton.setPreferredSize(new java.awt.Dimension(165, 80));
        MakeSellButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MakeSellButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(QueryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(61, 61, 61)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(SubTotalTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(IVATextField, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(TotalTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(MakeSellButton, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(IdFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(NameTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 1026, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(QuantyFormattedTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel3)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(461, 461, 461)
                                .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1177, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {IVATextField, SubTotalTextField, TotalTextField});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(IdFormattedTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                    .addComponent(NameTextField1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(QuantyFormattedTextField3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(QueryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(SubTotalTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(IVATextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(TotalTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(MakeSellButton, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(16, 16, 16))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 1209, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 759, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
//        String nom=this.NameTextField1.getText();
//        if(!(nom.length()<51)){
//        if(buscon.MakeQuery(identi) != null){
        if(!IdFormattedTextField.getText().equals("")&&!QuantyFormattedTextField3.getText().equals("")){
        
        }
        
        
        if(QuantyFormattedTextField3.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Tienes que llenar Cantidad", "advertencia", JOptionPane.OK_OPTION);
        }
        else if(IdFormattedTextField.getText().equals("")&& NameTextField1.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Faltan Datos por llenar", "advertencia", JOptionPane.OK_OPTION);
        }else if(IdFormattedTextField.getText().equals("")&&!NameTextField1.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Faltan Datos por llenar", "advertencia", JOptionPane.OK_OPTION);
        }else if(!IdFormattedTextField.getText().equals("")){
               Long identi=Long.parseLong(this.IdFormattedTextField.getText());
              
                    if(!NameTextField1.getText().equals("")){ 
                    resultado= buscon.verifyProductToSell(identi,NameTextField1.getText(), Integer.parseInt(this.QuantyFormattedTextField3.getText()));
                    float price=buscon.MakeQuery(identi,NameTextField1.getText()).getPrecioProducto();
                    float totalPrice=price*Integer.parseInt(this.QuantyFormattedTextField3.getText());
                    if(!resultado.equals("0")){
                         JOptionPane.showMessageDialog(null, resultado, "advertencia", JOptionPane.OK_OPTION);
                        if(resultado.toCharArray()[0]=='Q'){
                            tableModel.add(new RegVenta(Long.parseLong(this.IdFormattedTextField.getText()),this.NameTextField1.getText(), Integer.parseInt(this.QuantyFormattedTextField3.getText()), price, totalPrice, new JButton(), new JButton(), tableModel));
                            SumTotal(totalPrice,true);
                            TableProducts.updateUI();
                            cleanFields();
                        }   
                    }else{
                        tableModel.add(new RegVenta(Long.parseLong(this.IdFormattedTextField.getText()),this.NameTextField1.getText(), Integer.parseInt(this.QuantyFormattedTextField3.getText()), price, totalPrice, new JButton(), new JButton(), tableModel));
                        SumTotal(totalPrice,true);
                        TableProducts.updateUI();
                        cleanFields();
                    }
               }else if(NameTextField1.getText().equals("")){   
                resultado=buscon.verifyProductToSell(identi, Integer.parseInt(this.QuantyFormattedTextField3.getText()));
                float price=buscon.MakeQuery(identi).getPrecioProducto();
                float totalPrice=price*Integer.parseInt(this.QuantyFormattedTextField3.getText());
                if(!resultado.equals("0")){
                    JOptionPane.showMessageDialog(null, resultado, "advertencia", JOptionPane.OK_OPTION);
                if(resultado.toCharArray()[0]=='Q'){
                   tableModel.add(new RegVenta(Long.parseLong(this.IdFormattedTextField.getText()),NameTextField1.getText(), Integer.parseInt(this.QuantyFormattedTextField3.getText()), price, totalPrice, new JButton(), new JButton(), tableModel));
                   SumTotal(totalPrice,true);
                   TableProducts.updateUI();
                   cleanFields();
                }
                }else{
                    tableModel.add(new RegVenta(Long.parseLong(this.IdFormattedTextField.getText()),this.NameTextField1.getText(), Integer.parseInt(this.QuantyFormattedTextField3.getText()), price, totalPrice, new JButton(), new JButton(), tableModel));
                    SumTotal(totalPrice,true);
                    TableProducts.updateUI();
                    cleanFields();
                }
                }
        }
//        }else{
//        JOptionPane.showMessageDialog(null,"menos de 50 caracteres","advertencia",JOptionPane.OK_OPTION);
//        }
      
            
    }//GEN-LAST:event_AddButtonActionPerformed

    private void QueryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_QueryButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_QueryButtonActionPerformed

    private void IVATextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IVATextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_IVATextFieldActionPerformed

    private void NameTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_NameTextField1KeyPressed
//          List<String> noms;
//          LisProductsModel.removeAllElements();
//          if(this.NameTextField1.getText().length()>3){
//             noms= buscon.DedicatedSearch(this.NameTextField1.getText());   
//            
//             for(String n:noms){
//                LisProductsModel.addElement(n);
//             }
//            listProducts.setModel(LisProductsModel);
//            jPanel1.setLayout(new BorderLayout());
//            this.jPanel1.add(listProducts);
//            listProducts.setLocation(NameTextField1.getX(), NameTextField1.getY()+NameTextField1.getPreferredSize().width);
//            listProducts.setVisible(true);
//          }
          
    }//GEN-LAST:event_NameTextField1KeyPressed

    private void IdFormattedTextFieldMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_IdFormattedTextFieldMouseMoved
        if (!this.IdFormattedTextField.getText().isEmpty()){
            String s=buscon.SearchNameById(Long.parseLong(this.IdFormattedTextField.getText()));
            
            if(NameTextField1.getText().isEmpty()){
                this.NameTextField1.setText(s);
            }else if(!NameTextField1.getText().equals(s)){
                this.NameTextField1.setText(s);    
            }
        }
    }//GEN-LAST:event_IdFormattedTextFieldMouseMoved

    private void NameTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_NameTextField1KeyReleased
      
        if(evt.getKeyCode()!=8 && evt.getKeyCode()!= 127){
             if(!NameTextField1.getText().isEmpty())
                try {
                    aCControl.AddTextToTextField(NameTextField1,NameTextField1.getText(),true);
                } catch (BadLocationException ex) {
                    Logger.getLogger(SellGui.class.getName()).log(Level.SEVERE, null, ex);
             }
        }     
    }//GEN-LAST:event_NameTextField1KeyReleased

    private void NameTextField1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_NameTextField1FocusLost
        Object Id=buscon.SearchIdByName(NameTextField1.getText());        
        if(!Id.equals(-1)){
           IdFormattedTextField.setText(""+Id);
        }else{
            JOptionPane.showMessageDialog(null, "Ops.. no se encuentra producto", "advertencia", JOptionPane.OK_OPTION);
        }
    }//GEN-LAST:event_NameTextField1FocusLost

    private void TotalTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TotalTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TotalTextFieldActionPerformed

    private void MakeSellButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MakeSellButtonActionPerformed
        boolean bandera=false;
        for (RegVenta rv : tableModel.getProducts()) {
            Long Id = rv.getId();
            int quantity = rv.getQuantity();
            buscon.MakeSell(Id, quantity);

        }
        for (int i = 0; i <= tableModel.getRowCount(); i++) {
            tableModel.delete(i);
            TableProducts.updateUI();
            bandera=true;

        }
        if (!tableModel.getProducts().isEmpty()) {
            tableModel.delete(0);
            TableProducts.updateUI();
            bandera=true;
        }


        this.IVATextField.setText("0");
        this.SubTotalTextField.setText("0");
        this.TotalTextField.setText("0");

        buscon.newFacture(LoginGui.persL.getIdPersona(), Float.parseFloat(TotalTextField.getText()));
        
        if (bandera)
         JOptionPane.showMessageDialog(null, "VENTA EXITOSA", "URRA!!!", JOptionPane.OK_OPTION, new ImageIcon(getClass().getResource("/Icons/happyFace.png")));
    }//GEN-LAST:event_MakeSellButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JFormattedTextField IVATextField;
    private javax.swing.JFormattedTextField IdFormattedTextField;
    private javax.swing.JButton MakeSellButton;
    private javax.swing.JFormattedTextField NameTextField1;
    private javax.swing.JFormattedTextField QuantyFormattedTextField3;
    private javax.swing.JButton QueryButton;
    private javax.swing.JFormattedTextField SubTotalTextField;
    private javax.swing.JTable TableProducts;
    private javax.swing.JFormattedTextField TotalTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private void fillButtons() {
        TableProducts.setLayout(new FlowLayout());
        TableColumn tablecol0;
        tablecol0= TableProducts.getColumnModel().getColumn(0);
        tablecol0.setPreferredWidth(20);
        TableColumn tablecol1;
        tablecol1= TableProducts.getColumnModel().getColumn(1);
        tablecol1.setPreferredWidth(600);
        TableColumn tablecol2;
        tablecol2= TableProducts.getColumnModel().getColumn(2);
        tablecol2.setPreferredWidth(15);
        TableColumn tablecol5;
        tablecol5= TableProducts.getColumnModel().getColumn(5);
        System.out.println(TableProducts.getColumnModel());
        tablecol5.setCellRenderer(new MyRender());
        tablecol5.setCellEditor(new MyEditor(TableProducts,this));
        tablecol5.setPreferredWidth(40);
        TableColumn tablecol6;
        tablecol6= TableProducts.getColumnModel().getColumn(6);
        tablecol6.setCellRenderer(new MyRender());
        tablecol6.setCellEditor(new MyEditor(TableProducts,this));
        tablecol6.setPreferredWidth(40);
        TableProducts.setRowHeight(40);
        
        
    }
    //
    public void SumTotal(float totalPrice, boolean isaAdding) {
        if(isaAdding){
            float sum= Float.parseFloat(TotalTextField.getText())+totalPrice;
            float sumWithoutIVA=sum*0.84f;
            float IVA=sum*0.16f;
            float Tot=sumWithoutIVA+IVA;
            this.SubTotalTextField.setText(""+sumWithoutIVA);
            this.IVATextField.setText(""+IVA);
            this.TotalTextField.setText(""+Tot);
        }else{
            float sum= Float.parseFloat(TotalTextField.getText())-totalPrice;
            float sumWithoutIVA=sum*0.84f;
            float IVA=sum*0.16f;
            float Tot=sumWithoutIVA+IVA;
            this.SubTotalTextField.setText(""+sumWithoutIVA);
            this.IVATextField.setText(""+IVA);
            this.TotalTextField.setText(""+Tot);
        }
    }

    private void cleanFields() {
        this.IdFormattedTextField.setText("");
        this.NameTextField1.setText("");
        this.QuantyFormattedTextField3.setText("");
    }

    private void Integrity() {
        new RegexFormatter(RegularExpression.NATURAL_NUMBERS).install(IdFormattedTextField);
        new RegexFormatter(RegularExpression.NATURAL_NUMBERS).install(QuantyFormattedTextField3);
//        new RegexFormatter(RegularExpression.WORDS_AND_NUMBERS).install(NameTextField1);
    }
}
